import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import WindiCSS from 'vite-plugin-windicss'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
const env = loadEnv(process.env.NODE_ENV as string, process.cwd())


export default defineConfig({
    plugins: [
        vue(),
        vueJsx(({
            babelPlugins: [
                ['@babel/plugin-proposal-decorators', { legacy: true }], // vue 类写法
                // ['@babel/plugin-transform-flow-strip-types'],
                ['@babel/plugin-proposal-class-properties', { loose: true }], // Prop 装饰器 需要 否则导致报错
                // ['@babel/plugin-transform-class-static-block']
            ]
        })),
        WindiCSS(),
        AutoImport({
            resolvers: [AntDesignVueResolver({
                importStyle: false,
            })],
        }),
        Components({
            resolvers: [AntDesignVueResolver({
                importStyle: false,
            })],
        }),
    ],
    base:'./',
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        }
    },
    server: {
        host: '0.0.0.0',
        port: 5177,
        proxy: env.VITE_APP_PROXY_API ? ({
            '/crm-prod-v2': {
                target: env.VITE_APP_PROXY_APIV2,
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/crm-prod-v2/, ''),
            },
            '/crm-prod': {
                target: env.VITE_APP_PROXY_API,
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/crm-prod/, ''),
            },
            '/ws': {
                target: 'https://apis.map.qq.com/',
                changeOrigin: true,
            }
        }) : undefined
    }
})
